package com.heartware.sh.blackhole.ui.activity.base;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.heartware.sh.blackhole.R;
import com.heartware.sh.blackhole.common.utils._Toast;
import com.widget.dialog.CommonDialogFragment;
import com.widget.dialog.PromptDialogFragment;
import com.widget.toolbar.ToolBarX;

/**
 * Created by Administrator on 2016/11/24 0024.
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected Context mContext;

    private Toolbar mToolbar;
    private LinearLayout ll_content;

    private ToolBarX toolBarX;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        mContext = this;

        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);

        /**
         * 沉浸式状态栏...  最小SDK19
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            //透明导航栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

        mToolbar = (Toolbar) super.findViewById(R.id.toolbar);
        ll_content = (LinearLayout) super.findViewById(R.id.ll_content);

        setContentView(getLayoutId());

        initView();
        setListener();

    }

    /**
     * 抽象方法,子类实现, 返回资源ID
     * @return
     */
    public abstract int getLayoutId();

    /**
     * 初始化控件..
     */
    public void initView(){}


    /**
     * 设置监听器.
     */
    public void setListener(){}


    /**
     * 获取ToolBarX
     * @return
     */
    public ToolBarX getToolBarX(){

        if(toolBarX == null){
            toolBarX = new ToolBarX(this, mToolbar);
        }

        return toolBarX;
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
//        super.setContentView(layoutResID);
        setContentView(View.inflate(this, layoutResID, null));
    }

    @Override
    public void setContentView(View view) {
//        super.setContentView(view);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        ll_content.addView(view, params);
    }

    @Override
    public View findViewById(@IdRes int id) {
        return ll_content.findViewById(id);
    }

    public void showToast(Context mContext, int resId){
        _Toast.showToast(mContext, resId);
    }

    public void showToast(Context mContext, String str){
        _Toast.showToast(mContext, str);
    }

    /**
     * 通用单按钮对话框...
     * @param title
     * @param btnText
     */
    public void showPromptDialog(String title, String content,String btnText, PromptDialogFragment.onDialogButtonClickListener listener) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment fragment = getSupportFragmentManager().findFragmentByTag("prompDialog");
        if (fragment != null) {
            ft.remove(fragment);
        }
        ft.addToBackStack(null);
        PromptDialogFragment promptfragment = PromptDialogFragment.newInstance(title, content, btnText);
        promptfragment.show(ft, "prompDialog");
        promptfragment.setOnButtonClickListener(listener);
    }

    /**
     * 双按钮dialog;
     * @param title
     * @param content
     * @param leftBtnName
     * @param rightBtnName
     * @param listener
     */
    public void showCommonDialog(String title,  String content, String leftBtnName, String rightBtnName, CommonDialogFragment.onDialogButtonClickListener listener) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment fragment = getSupportFragmentManager().findFragmentByTag("commonDialog");
        if (fragment != null) {
            ft.remove(fragment);
        }
        ft.addToBackStack(null);
        CommonDialogFragment commonFragment = CommonDialogFragment.newInstance(title, content, leftBtnName, rightBtnName);
        commonFragment.setOnButtonClickListener(listener);
        commonFragment.show(ft, "commonDialog");
    }

}
